﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VersionComparer.Views
{
    public class AutoCompleteCustomSource : AutoCompleteStringCollection
    {
        List<string> knownDirs = new List<string>();
        public bool Update(string text)
        {
            string []files = text.Split ( ';');
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < files.Length - 1; i++)
                builder.AppendFormat("{0};", files[i]);

            try
            {
                string directory = Path.GetDirectoryName(files.Last());
                string fullDir = string.Format("{0}{1}", builder, directory);
                if (string.IsNullOrEmpty(directory))
                    return false;
                if (!knownDirs.Contains(fullDir))
                {
                    try
                    {
                        if (Directory.Exists(directory))
                        {
                            List<string> filestoUse = new List<string>(Directory.GetFiles(directory));
                            filestoUse.AddRange(Directory.GetDirectories(directory));
                            string[] aux = new string[filestoUse.Count];
                            for (int i = 0; i < filestoUse.Count; i++)
                                aux[i] = string.Format("{0}{1}", builder, filestoUse[i]);
                            knownDirs.Add(fullDir);
                            this.AddRange(aux);
                        }
                        else
                            return false;
                    }
                    catch
                    {
                        return false;
                    }
                    
                }
                return true;
            }
            catch { }
            return false;
        }
    }
}